/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.games.epkb.controller;

import it.polimi.games.epkb.controller.exceptions.NonexistentEntityException;
import it.polimi.games.epkb.entityClasses.FileUsage;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import it.polimi.games.epkb.entityClasses.Activity;
import javax.transaction.UserTransaction;

/**
 *
 * @author plebani
 */
public class FileUsageJpaController implements Serializable {

    public FileUsageJpaController(EntityManagerFactory emf) {
        this.utx = null;
        this.emf = emf;
    }

    public FileUsageJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(FileUsage fileUsage) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Activity idActivity = fileUsage.getIdActivity();
            if (idActivity != null) {
                idActivity = em.getReference(idActivity.getClass(), idActivity.getId());
                fileUsage.setIdActivity(idActivity);
            }
            em.persist(fileUsage);
            if (idActivity != null) {
                idActivity.getFileUsageCollection().add(fileUsage);
                idActivity = em.merge(idActivity);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(FileUsage fileUsage) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FileUsage persistentFileUsage = em.find(FileUsage.class, fileUsage.getId());
            Activity idActivityOld = persistentFileUsage.getIdActivity();
            Activity idActivityNew = fileUsage.getIdActivity();
            if (idActivityNew != null) {
                idActivityNew = em.getReference(idActivityNew.getClass(), idActivityNew.getId());
                fileUsage.setIdActivity(idActivityNew);
            }
            fileUsage = em.merge(fileUsage);
            if (idActivityOld != null && !idActivityOld.equals(idActivityNew)) {
                idActivityOld.getFileUsageCollection().remove(fileUsage);
                idActivityOld = em.merge(idActivityOld);
            }
            if (idActivityNew != null && !idActivityNew.equals(idActivityOld)) {
                idActivityNew.getFileUsageCollection().add(fileUsage);
                idActivityNew = em.merge(idActivityNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = fileUsage.getId();
                if (findFileUsage(id) == null) {
                    throw new NonexistentEntityException("The fileUsage with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FileUsage fileUsage;
            try {
                fileUsage = em.getReference(FileUsage.class, id);
                fileUsage.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The fileUsage with id " + id + " no longer exists.", enfe);
            }
            Activity idActivity = fileUsage.getIdActivity();
            if (idActivity != null) {
                idActivity.getFileUsageCollection().remove(fileUsage);
                idActivity = em.merge(idActivity);
            }
            em.remove(fileUsage);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<FileUsage> findFileUsageEntities() {
        return findFileUsageEntities(true, -1, -1);
    }

    public List<FileUsage> findFileUsageEntities(int maxResults, int firstResult) {
        return findFileUsageEntities(false, maxResults, firstResult);
    }

    private List<FileUsage> findFileUsageEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(FileUsage.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public FileUsage findFileUsage(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(FileUsage.class, id);
        } finally {
            em.close();
        }
    }

    public int getFileUsageCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<FileUsage> rt = cq.from(FileUsage.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
